SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE [Weekly].[GetPricingSources] --@FUND VARCHAR(10)
	@FundID VARCHAR(50)
	, @processDate DATETIME = null

AS
BEGIN
	
	SET NOCOUNT ON;

	DECLARE @exportID INT
	SET @exportID = CASE WHEN @processDate IS NULL
							THEN (SELECT MAX(ex.ExportID) FROM FSFundModel.Reporting.Export ex WHERE ex.FundID = @FundID)
						ELSE (SELECT MAX(ex.ExportID) FROM FSFundModel.Reporting.Export ex WHERE NAVDate = @processDate AND ex.FundID = @FundID)
					END
	         

	SELECT     ca.PricingVendor AS 'PricingVendor', SUM(a.MktVal) MktVal


 FROM  FSFundModel.Reporting.Asset a 
 INNER JOIN  FSFundModel.Reporting.Instrument i  ON 
 a.ExportId = i.ExportId 
 AND a.InstrumentId = i.InstrumentId

 CROSS APPLY(SELECT REPLACE(i.PricingMethodType + ' - To Be ' + i.PricingVendor,'Vendor - To Be ', '') PricingVendor) ca


WHERE a.IsActiveEndOfDay= 1

AND a.ExportId = @exportID
GROUP BY ca.PricingVendor 
ORDER BY ca.PricingVendor ASC 



END


GO
EXEC sp_addextendedproperty N'VirtualFolder', N'Weekly', 'SCHEMA', N'Weekly', 'PROCEDURE', N'GetPricingSources', NULL, NULL
GO
